﻿
namespace NetAssist.IO
{
  /// <summary>
  /// Implements a FileBlock that reads blocks of data from a stream.
  /// </summary>
  public class BinaryFileBlock : FileBlock
  {
    /// <summary>
    /// Initializes a new instance of the NetAssist.IO.BinaryFileBlock class.
    /// </summary>
    /// <param name="blockIndex">Indicates the zero based index of the block in the stream/file.</param>
    /// <param name="startPosition">The distance of the start of the block in bytes from the start of the file.</param>
    /// <param name="content">The content of the block.</param>
    public BinaryFileBlock(int blockIndex, int startPosition, byte[] content)
      : base(blockIndex, startPosition)
    {
      this.Content = content;
    }

    /// <summary>
    /// Gets the content of the block.
    /// </summary>
    public byte[] Content { get; private set; }

    /// <summary>
    /// The distance of the end of the block in bytes from the start of the file.
    /// </summary>
    public override int EndPosition
    {
      get { return base.StartPosition + this.Content.Length - 1; }
    }

    /// <summary>
    /// The actual length of the block.
    /// </summary>
    public override int Length
    {
      get { return this.Content.Length; }
    }

    /// <summary>
    /// Converts the binaryFileBlock into a byte array.
    /// </summary>
    /// <param name="binaryFileBlock">The BinaryFileBlock to convert.</param>
    /// <returns>A byte array representing the content of the block.</returns>
    public static implicit operator byte[](BinaryFileBlock binaryFileBlock)
    {
      Helper.InvalidateNullParam(binaryFileBlock, "binaryFileBlock");
      return binaryFileBlock.Content;
    }

    /// <summary>
    /// Converts the binaryFileBlock into a byte array.
    /// </summary>
    /// <param name="binaryFileBlock">The BinaryFileBlock to convert.</param>
    /// <returns>A byte array representing the content of the block.</returns>
    public static byte[] FromBinaryFileBlock(BinaryFileBlock binaryFileBlock)
    {
      return binaryFileBlock;
    }
  }
}
